package com.zero.mms.controller;

import java.util.List;






import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.neusoft.shry.dto.PositionDto;
import com.neusoft.shry.service.PositionService;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.basic.model.type.PositionType;
import com.zero.basic.util.JsonCreator;
import com.zero.mms.model.Dep;
import com.zero.mms.model.Employee;
import com.zero.mms.model.Position;

@Controller("positionController")
@RequestMapping("position")
public class PositionController {
	
	@Inject
	private PositionService positionService;
	
	@RequestMapping("/searchPositions")
	@ResponseBody
	public Object getPositions(String dep_id_string,String dep_name,String position_id_string,String position_name,String position_type){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer dep_id;
		Integer position_id;
		
		System.out.println(dep_id_string+dep_name+position_id_string+position_name+position_type);
		
		if("*".equals(dep_id_string)||"".equals(dep_id_string)){
			dep_id = null;
		}else{
			dep_id = Integer.parseInt(dep_id_string);
		}
		
		if("*".equals(position_id_string)||"".equals(position_id_string)){
			position_id = null;
		}else{
			position_id = Integer.parseInt(position_id_string);
		}
		
		try {
			System.out.println(positionService);
			Pager<PositionDto> pager = positionService.getPositionsByConditions(dep_id, dep_name, position_id, position_name, position_type);
			objectNode.putPOJO("response", pager.getDatas());
			objectNode.put("page_num", pager.getOffset()/pager.getSize()+1);
			System.out.println(pager.getOffset());
			System.out.println(pager.getOffset()/pager.getSize()+1);
			objectNode.put("total_page", (pager.getTotal()%pager.getSize())>0?pager.getTotal()/pager.getSize()+1:pager.getTotal()/pager.getSize());
			objectNode.put("total_num", pager.getTotal());
			objectNode.put("page_size", pager.getSize());
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/addPosition")
	@ResponseBody
	public Object addPosition(String position_name,String dep_id_string,String position_type,String position_maxmember_string){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		System.out.println(position_name+dep_id_string+position_type+position_maxmember_string);
		
		Integer dep_id = Integer.parseInt(dep_id_string);
		Integer position_maxmember;
		if("".equals(position_maxmember_string)){
			position_maxmember = null;
		}else{
			position_maxmember = Integer.parseInt(position_maxmember_string);
		}
		
		Position position = new Position();
		position.setPositionName(position_name);
		position.setDepId(dep_id);
		position.setPositionType(position_type);
		position.setPositionMax(position_maxmember);
		
		System.out.println("add a position----------");
		System.out.println(position_name+dep_id+position_type+position_maxmember);
		
		try {
			positionService.addPosition(position);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "添加岗位成功");
			objectNode.put("messageType", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		
		return objectNode;
	}
	
	@RequestMapping("/updatePosition")
	@ResponseBody
	public Object updatePosition(String position_id_string,String position_name,String dep_id_string,String position_type,String position_maxmember_string){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer dep_id;
		Integer position_id;
		Integer position_maxmember;
		
		if("*".equals(dep_id_string)||"".equals(dep_id_string)){
			dep_id = null;
		}else{
			dep_id = Integer.parseInt(dep_id_string);
		}
		
		if("*".equals(position_id_string)||"".equals(position_id_string)){
			position_id = null;
		}else{
			position_id = Integer.parseInt(position_id_string);
		}
		
		if("".equals(position_maxmember_string)){
			position_maxmember = null;
		}else{
			position_maxmember = Integer.parseInt(position_maxmember_string);
		}
		
		try {
			positionService.updatePosition(position_id, position_name, dep_id, position_type, position_maxmember);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "修改岗位成功");
			objectNode.put("messageType", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/deletePosition")
	@ResponseBody
	public Object deletePosition(String position_id_string){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer position_id;
		
		if("*".equals(position_id_string)||"".equals(position_id_string)){
			position_id = null;
		}else{
			position_id = Integer.parseInt(position_id_string);
		}
		
		try {
			positionService.deletePosition(position_id);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "删除岗位成功");
			objectNode.put("messageType", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/viewEmpInPosition")
	@ResponseBody
	public Object viewEmpInPosition(String position_id_string){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer position_id;
		
		if("*".equals(position_id_string)||"".equals(position_id_string)){
			position_id = null;
		}else{
			position_id = Integer.parseInt(position_id_string);
		}
		
		try {
			List<Employee> empList = positionService.viewEmployeesInPosition(position_id);
			objectNode.putPOJO("response", empList);
			objectNode.putPOJO("total_num", empList.size());
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/viewPosition")
	@ResponseBody
	public Object viewPosition(String position_id_string){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		Integer position_id;
		
		if("*".equals(position_id_string)||"".equals(position_id_string)){
			position_id = null;
		}else{
			position_id = Integer.parseInt(position_id_string);
		}
		
		try {
			Position position = positionService.viewPosition(position_id);
			objectNode.putPOJO("position_id", position.getPositionId());
			objectNode.putPOJO("position_name", position.getPositionName());
			objectNode.putPOJO("position_type", position.getPositionType());
			objectNode.putPOJO("position_max", position.getPositionMax());
			objectNode.put("dep_id", position.getDepId());
			
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/getSelectList")
	@ResponseBody
	public Object getSelectList(){
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		
		try {
			List<Dep> deps = positionService.getAllDeps();
			objectNode.putPOJO("depList", deps);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("messageType", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("messageType", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
}
